var id = "";
var addForm = "";
var checkFlag = true;
var parentCode='';	
$(function(){
	parentCode = request.QueryString("parentCode");// 点击左侧菜单传过来的按钮父节点
	id = request.QueryString("id");// 从跳转页面过来的
	addForm = $("#addForm");
	initCombobox();
	tree();
	$("#tt").tree("expandAll");
	$("#btncancel").click(function() {
		tree();
		addForm.form('reset');
		addForm.form("disableValidation");
	});
	addForm.form("disableValidation");

});

function initCombobox() {
	// 初始化状态下拉菜单
	$('#status').combobox({
		editable : false, // 不可编辑状态
		cache : false,
		panelHeight : 'auto',// 自动高度适合
		valueField : 'code',
		textField : 'name'
	});
	// 数据放入下拉菜单
	var ut = postAsyncAjax("/findCodebySortCode?sortCode=004", null);// 获取状态
	if (ut.data == null)
		ut.data = [];
	ut.data.unshift({
		"code" : "",
		"name" : "--请选择--"
	});
	$("#status").combobox("clear");
	$("#status").combobox("loadData", ut.data);

	// 初始化状态下拉菜单
	$('#identifying').combobox({
		editable : false, // 不可编辑状态
		cache : false,
		panelHeight : 'auto',// 自动高度适合
		valueField : 'code',
		textField : 'name'
	});
	// 数据放入下拉菜单
	var ut = postAsyncAjax("/findCodebySortCode?sortCode=050", null);// 获取角色标识
	if (ut.data == null)ut.data = [];
	for(var i =0; i < ut.data.length; i++){
		if('admin' == ut.data[i].code){
			ut.data.splice(i,1);
		}
	}
	ut.data.unshift({"code" : "","name" : "--请选择--"});
	$("#identifying").combobox("clear");
	$("#identifying").combobox("loadData", ut.data);
}

/**
 * 刷新grid
 */
function reloadGird() {
	gridReload("roleList");
}

function addBankAccount() {
	var nodes = $('#tt').tree('getChecked', [ 'checked', 'indeterminate' ]);
	var arr = new Array();
	for (i in nodes) {
		arr.push(nodes[i].id)
	}
	var described = arr.toString();

	if (addForm.form('enableValidation')) {
		addForm.form('submit', {
			url : '/addRole?described=' + described,
			success : function(msg) {
				msg = $.parseJSON(msg);
				if (msg.returnCode == '02') {
					top.showMessage("<font color='red'>角色名称已存在!</font>!");
					$("#name").focus();
					return;
				}
				if (msg.returnCode == '00') {
					$.messager.alert('提示','添加成功！','info',function(){
						var menutab = top.$("#headFrame").get(0).contentWindow.$("#menutab");
						menutab.tabs('getSelected').panel('refresh');
					});
				} else {
					top.showMessage("添加失败！");
				}
			}
		});
	}
}

function tree() {
	var state = "";
	var t = 1;
	var param = [];
	var limit = postAsyncAjax("/findLimitsAll", null).data;
	for (var i = 0; i < limit.length; i++) {
		if (limit[i].parentCode == "-1") {
			param.push({
				"id" : limit[i].id,
				"text" : limit[i].name,
				"state" : "closed",
				"iconCls" : "icon-group",
				"code" : limit[i].code,
				"parentCode" : limit[i].parentCode,
				"t" : t
			})
			break;
		}
	}

	$("#tt").tree({
		data : param,
		checkbox : true,
		cascadeCheck : false,
		required : true,
		lines : true,
		onCheck : function(node, checked) {
			// 选中所有子节点
			if (checkFlag) {
				checkFlag = false;
				if (checked) {
					getAllChilds(node, 'check');
				} else {
					getAllChilds(node, 'uncheck');
				}
				// 选中所有父节点
				if (checked) {
					getAllParents(node, 'check');
				}
				checkFlag = true;

			}

		},
		onBeforeExpand : function(note) {
			if (note.t == 1 || !note.state) {
				userTreeLoad(note);
			}
		}
	});
	// 获取所有子节点并操作
	function getAllChilds(node, checked) {

		var curChilds = node.children;

		if (curChilds && curChilds.length > 0) {
			for (var i = 0; i < curChilds.length; i++) {
				var child = curChilds[i];
				$('#tt').tree(checked, child.target);
				getAllChilds(child, checked);
			}

		}

	}
	// 获取所有父节点并操作
	function getAllParents(node, checked) {
		var cuparent = $('#tt').tree('getParent', node.target);
		if (cuparent) {
			$('#tt').tree(checked, cuparent.target);
			getAllParents(cuparent, checked);
		}

	}

	function userTreeLoad(note) {
		var param = new Array();
		for (var i = 0; i < limit.length; i++) {
			if (limit[i].parentCode == note.code) {
				var lcolor = 'black';
				if (limit[i].type == 'button') {
					lcolor = 'green';
				}
				if (limit[i].type == 'method') {
					lcolor = 'red';

				}
				param.push({
					"id" : limit[i].id,
					"text" : '<font color="' + lcolor + '">' + limit[i].name
							+ '</font>',
					"state" : "closed",
					"code" : limit[i].code,
					"parentCode" : limit[i].parentCode,
					"t" : t
				})
			}
		}
		$('#tt').tree('append', {
			parent : note.target,
			data : param
		});
		note.t = 2;
	}
	$("#tt").tree("expandAll");
	$("#tt").tree("expandAll");
	$("#tt").tree("expandAll");
	$("#tt").tree("expandAll");
}
